use std::{collections::HashMap, io::{BufRead, BufReader}};

pub fn check_id_card(id_card_no: &str) -> String {
    let file_source = std::fs::File::open("src/table.rs").unwrap();
    let  buf_source = BufReader::new(file_source);

    // let file = std::fs::File::open("province.table")?;
    // let mut buf = BufReader::new(file);
    // let target = std::fs::File::create("erjishi.table")?;
    // let mut tar = BufWriter::new(target);

    let mut source_lines = HashMap::new();
    for b in buf_source.lines(){
        let b_line = b.unwrap();
        let part_b: Vec<&str> = b_line.split_whitespace().collect();
        let number_b = part_b[0];
        source_lines.insert(number_b.to_string(), part_b[1].to_string());
    }
    let mut res = String::new();

    id_is_correcet(id_card_no, &source_lines, &mut res);
    if parse(id_card_no, &source_lines)!=true {
        return res;
    }

    add_sex(&mut res, id_card_no);

    add_birthday(id_card_no, &mut res);

    add_location(&mut res, id_card_no, source_lines);

    res
}
// 添加地址哈 
fn add_location(res: &mut String, id: &str, source_lines: HashMap<String, String>) {
    *res += ",";
    let key = format!("{}{}", &id[..2], "0000");

    if let Some(value) = source_lines.get(key.as_str()) {
        *res += value;
        *res += "-";
    }
    let key = format!("{}{}", &id[..4], "00");
    if let Some(value) = source_lines.get(key.as_str()) {
        *res += value;
        *res += "-";
    }

    let key = &id[..6];
    if let Some(value) = source_lines.get(key) {
        *res += value;
    }
}

fn add_birthday(id: &str, res: &mut String) {
    *res += ",";

    if id.len() == 18 {
        let tmp = format!("{}年{}月{}日", &id[6..10], &id[10..12], &id[12..14]);
        *res += tmp.as_str();
    } else {
        let tmp = format!("19{}年{}月{}日", &id[6..8], &id[8..10], &id[10..12]);
        *res += tmp.as_str();
    }
}

fn id_is_correcet(id: &str, source_lines: &HashMap<String, String>, res: &mut String) {
    if parse(id, source_lines) {
        *res += "身份证号码正确";
        // return res;
    } else {
        *res += "身份证号码错误";
        // return res;
    }
}

fn add_sex(res: &mut String, id: &str) {
    *res += ",";

    if id.len() == 18 {
        if &id[16..17].to_string().parse::<i32>().unwrap() % 2 == 1 {
            *res += "男";
        } else {
            *res += "女";
        }
    } else {
        if &id[14..].to_string().parse::<i32>().unwrap() % 2 == 1 {
            *res += "男";
        } else {
            *res += "女";
        }
    }
}
// pub fn check_id_card(id_card_no: &str) -> String {

//     let mut res = "身份证号码正确".to_string();
//     res+= "fa";
//     println!("{}",res);
// }

fn parse(id: &str, table: &HashMap<String, String>) -> bool {
    let len_tmp = id.len();
    match len_tmp {
        15 => {
            if let None = table.get(&id[..6]) {
                return false;
            }
        }
        18 => {
            if let None = table.get(&id[..6]) {
                return false;
            }
            let ve_char: Vec<_> = (&id[..17])
                .chars()
                .map(|s| s.to_digit(10).unwrap())
                .collect();
            let sum = ve_char[0] * 7
                + ve_char[1] * 9
                + ve_char[2] * 10
                + ve_char[3] * 5
                + ve_char[4] * 8
                + ve_char[5] * 4
                + ve_char[6] * 2
                + ve_char[7] * 1
                + ve_char[8] * 6
                + ve_char[9] * 3
                + ve_char[10] * 7
                + ve_char[11] * 9
                + ve_char[12] * 10
                + ve_char[13] * 5
                + ve_char[14] * 8
                + ve_char[15] * 4
                + ve_char[16] * 2;
            let num_18 = match sum % 11 {
                0 => "1",
                1 => "0",
                2 => "X",
                3 => "9",
                4 => "8",
                5 => "7",
                6 => "6",
                7 => "5",
                8 => "4",
                9 => "3",
                10 => "2",
                _ => "z",
            };
            if num_18 != &id[17..] {
                return false;
            }
        }
        _ => {
            return false;
        }
    }

    true
}
